package com.xzx.shop_front_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzx.shop_front_api.entity.Coupon;
import com.xzx.shop_front_api.entity.MemberCoupon;
import com.xzx.shop_front_api.entity.SearchHistory;
import com.xzx.shop_front_api.mapper.CouponMapper;
import com.xzx.shop_front_api.mapper.MemberCouponMapper;
import com.xzx.shop_front_api.mapper.SearchHistoryMapper;
import com.xzx.shop_front_api.service.CouponService;
import com.xzx.shop_front_api.service.SearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private Coupon coupon;
    @Autowired
    private MemberCoupon memberCoupon;
    @Autowired
    private MemberCouponMapper memberCouponMapper;

    @Override
    public int insertCoupon(Coupon coupon) {
        return couponMapper.insert(coupon);
    }

    @Override
    public List<Coupon> couponList(int currentTime, int status) {
        QueryWrapper<Coupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        queryWrapper.lt("start_time", currentTime);
        queryWrapper.gt("end_time", currentTime);
        queryWrapper.orderByDesc("created_time");
        return couponMapper.selectList(queryWrapper);
    }

    @Override
    public Coupon getCouponById(Integer couponId) {
        return couponMapper.selectById(couponId);
    }

    @Override
    public int updateCouponInfo(int couponId, int status, int currentTime, Long orderId) {
        memberCoupon.setStatus(status);
        memberCoupon.setUseTime(currentTime);
        memberCoupon.setOrderId(Math.toIntExact(orderId));
        QueryWrapper<MemberCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", couponId);
        return memberCouponMapper.update(memberCoupon,queryWrapper);
    }


}
